//
//  RHMePlantTopoCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/6.
//

#import "RHMePlantTopoCell.h"
#import "RHPlantTopoMEView2.h"
#import "RHMePlantTopoDevItemCell.h"
#import "RHMePlantDetailModel.h"

#define itemWidth  (kUI_WINDOW_W - 30)/3
@interface RHMePlantTopoCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong) RHPlantTopoMEView2 *meTopoView;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@property (nonatomic ,strong) RHMePlantDetailModel *detailModel;

@property (nonatomic ,assign) ProductType productType;//产口类型
@end
@implementation RHMePlantTopoCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

- (void)setDetailModel:(RHMePlantDetailModel *)detailModel productType:(ProductType)productType
{
    _detailModel = detailModel;
    _productType = productType;
    [self.meTopoView setDetailModel:detailModel productType:productType];
    CGFloat height = 0;
    CGFloat width = detailModel.deviceList.count%3;

    if (detailModel.deviceList.count<=3) {
        height = 110;
        width = itemWidth * detailModel.deviceList.count;
    }else{
        height = 110*2;
        width = itemWidth * 3;//3个以上
    
    }
    if (detailModel.deviceList.count == 0) {
        height = 0;
    }
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(width);
        make.height.offset(height);
    }];
    
    if (detailModel.deviceList.count>6) {
        self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }else{
        self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    
    [self.collectionView reloadData];
}

- (void)setupUI
{
    self.contentView.backgroundColor = CFTabViewBgColor;
    kWeakSelf;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.qmui_borderColor = kBorderColor;
    bgView.qmui_borderWidth = 1;
    bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionRight|QMUIViewBorderPositionTop|QMUIViewBorderPositionBottom;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(0);
    }];
    
    QMUILabel *titleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(20) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
    titleLab.text = @"电站拓扑图".rh_localized;
    [bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(15);
        make.right.mas_offset(-15);
    }];
    
    //状态标签
    UIView *statusView = kView;
    [bgView addSubview:statusView];
    [statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLab);
        make.right.mas_offset(-10);
    }];
    __block CGFloat statusLabelWidth = 0;
    __block QMUILabel *lastLab;
    NSArray *statusTitles = @[@"正常".rh_localized,@"离线".rh_localized,@"告警".rh_localized,@"故障".rh_localized];
    NSArray *colorArr = @[@"#05C79D",@"#8B8D92",@"#F0A330",@"#F95454"];
    for (int i = 0; i<statusTitles.count; i++) {
        QMUILabel *statusLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentCenter];
        statusLab.text = statusTitles[i];
        statusLab.textColor = kCOLOR_HEX(colorArr[i]);
        statusLab.layer.cornerRadius = 10;
        statusLab.layer.masksToBounds = YES;
        [statusLab sizeToFit];
        [statusView addSubview:statusLab];
        [statusLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(10);
            make.bottom.mas_offset(-10);
            if (lastLab) {
                make.left.equalTo(lastLab.mas_right).offset(10);
                statusLabelWidth += 10;
            }else{
                make.left.mas_offset(0);
            }
            make.width.offset(statusLab.width+20);
            make.height.offset(22);
            statusLabelWidth += statusLab.width+20;
        }];
        
        UIView *shadowView = kView;
        shadowView.backgroundColor = UIColor.whiteColor;
        shadowView.layer.shadowColor = kCOLOR_HEX_A(@"#000000", 0.5).CGColor;
        shadowView.layer.shadowRadius = 5;
        shadowView.layer.shadowOpacity = 0.5;
        shadowView.layer.cornerRadius = 10;
        shadowView.layer.shadowOffset = CGSizeMake(0, 0.5);
        [statusView addSubview:shadowView];
        [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(statusLab);
        }];
        [statusView bringSubviewToFront:statusLab];
        lastLab = statusLab;
        
    }
    [lastLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(0);
    }];
    
    [titleLab sizeToFit];
    if (titleLab.width> kUI_WINDOW_W - 60-20-statusLabelWidth) {
        [statusView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLab.mas_bottom).offset(10);
            make.right.mas_offset(-15);
        }];
    }
    
    _meTopoView = [[RHPlantTopoMEView2 alloc]init];
    [bgView addSubview:_meTopoView];
    [_meTopoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(statusView.mas_bottom).offset(0);
        make.left.right.mas_offset(0);
    }];
    
    _layout = [[UICollectionViewFlowLayout alloc]init];
    _layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
    [_collectionView registerClass:[RHMePlantTopoDevItemCell class] forCellWithReuseIdentifier:@"RHMePlantTopoDevItemCell"];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    [bgView addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(0);
        make.height.offset(0);
        make.bottom.mas_offset(0);
        make.top.equalTo(weakSelf.meTopoView.mas_bottom).offset(0);
        make.centerX.mas_offset(0);
    }];
    
    [bgView bringSubviewToFront:statusView];
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return self.detailModel.deviceList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHMePlantTopoDevItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RHMePlantTopoDevItemCell" forIndexPath:indexPath];
    cell.model = [self.detailModel.deviceList safeObjectAtIndex:indexPath.item];
    
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(itemWidth, 110);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.clickDeviceBlock) {
        self.clickDeviceBlock(self.detailModel.deviceList[indexPath.row]);
    }
}

@end
